What is unist-util-position?
The unist-util-position package is a utility for working with positional information in unist syntax trees. It provides functions to get and manipulate positions of nodes within these trees, which is useful for tasks such as error reporting, syntax highlighting, and more.
What are unist-util-position's main functionalities?
position.start
This feature retrieves the starting position of a node. The code sample demonstrates how to use the `position.start` function to get the starting line and column of a node.
const position = require('unist-util-position');
const node = { position: { start: { line: 1, column: 1 }, end: { line: 1, column: 5 } } };
console.log(position.start(node)); // { line: 1, column: 1 }
position.end
This feature retrieves the ending position of a node. The code sample shows how to use the `position.end` function to get the ending line and column of a node.
const position = require('unist-util-position');
const node = { position: { start: { line: 1, column: 1 }, end: { line: 1, column: 5 } } };
console.log(position.end(node)); // { line: 1, column: 5 }
position.point
This feature normalizes a point object. The code sample demonstrates how to use the `position.point` function to ensure a point object has the correct structure.
const position = require('unist-util-position');
const point = { line: 1, column: 1 };
console.log(position.point(point)); // { line: 1, column: 1 }
Other packages similar to unist-util-position
unist-util-visit
The unist-util-visit package is used to recursively walk through unist syntax trees. While it doesn't specifically handle positional information, it can be used in conjunction with unist-util-position to traverse nodes and then retrieve their positions.
unist-util-find
The unist-util-find package helps in finding nodes in unist syntax trees based on certain criteria. It can be used to locate nodes and then unist-util-position can be used to get their positional information.
unist-util-select
The unist-util-select package allows for selecting nodes in unist syntax trees using CSS-like selectors. Once nodes are selected, unist-util-position can be used to get their positions. This package is useful for more complex queries within the tree.
unist-util-position
unist utility to get the position of nodes.
Installation
npm:
npm install unist-util-position
unist-util-position is also available as an AMD, CommonJS, and
globals module, uncompressed and compressed.
Usage
var remark = require('remark');
var position = require('unist-util-position');
var ast = remark().parse([
'# foo',
'',
'* bar',
''
].join('\n'));
position.start(ast)
position.end(ast)
position.generated(ast)
position.start()
position.end()
position.generated()
API
position.start([node])
position.end([node])
Get the bound position of node
.
Parameters
node
(Node?
) — Node to check.
Returns
Position
— Filled with line
(nullable uint32 >= 1
),
column
(nullable uint32 >= 1
), offset
(nullable uint32 >= 0
).
position.generated([node])
Get the heading style of a node.
Parameters
node
(Node
) — Node to check;
Returns
boolean
— Whether or not node
has positional information (both
starting and ending lines and columns). This is useful when checking
if a node is inserted by plug-ins.
License
MIT © Titus Wormer